import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/assessment/assessmentRecordsResult.dart';
import 'package:ichinesereader/models/assessment/assessmentTeacherQueryDto.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/assessmentsRecordsDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

// ignore: must_be_immutable
class SingleAssessmentProgress extends StatefulWidget {
  SingleAssessmentProgress(
      {Key? key, required this.assessmentId, required this.name})
      : super(key: key);
  static const String routeName = '/teacher/reports/assessmentProgress';
  String assessmentId;
  String name;

  @override
  _AssessmentProgress createState() => _AssessmentProgress();
}

class _AssessmentProgress extends State<SingleAssessmentProgress> {
  final ReportsController _reportsController = Get.find();
  var box = Hive.box('appconfig');

  @override
  void initState() {
    var classId = box.get(CacheManagerKey['currentclass'].toString());
    Future.delayed(Duration(milliseconds: 100), () {
      AssessmentTeacherQueryDto dto = new AssessmentTeacherQueryDto(
          status: [], assessmentId: widget.assessmentId, type: []);
      _reportsController.fetchTeacherAssessmentRecords(classId, dto);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher assessment progress');

    return Scaffold(
      appBar: CustomAppBar(title: widget.name),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: Column(
            children: [
              _reportsController.obx(
                (data) {
                  if (_reportsController.teacherAssessmentRecord.value.list !=
                      null) {
                    return Container(
                        child: AssessmentsRecordsDataGrid(
                            _reportsController
                                .teacherAssessmentRecord.value.list!,
                            new AssessmentTeacherQueryDto(
                                status: [], type: [])));
                  } else {
                    return Text('');
                  }
                },
                onEmpty: Container(
                    child: AssessmentsRecordsDataGrid(<QuestionRecord>[],
                        new AssessmentTeacherQueryDto(status: [], type: []))),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
